package tp.pr4.instructions;

import java.util.StringTokenizer;
import tp.pr4.NavigationModule;
import tp.pr4.RobotEngine;
import tp.pr4.instructions.exceptions.InstructionExecutionException;
import tp.pr4.instructions.exceptions.WrongInstructionFormatException;
import tp.pr4.items.ItemContainer;

/**
 * This Instruction shows the description of the current place and the 
 * items in it. This Instruction works if the user writes RADAR
 * @author Alvaro Saez Hernando
 * @author Noel Clemente Montero 
 */
public class RadarInstruction implements Instruction{

	private static final String LINE_SEPARATOR = System.lineSeparator();
	private NavigationModule navigation;
	
	/**
	 * Parses the String returning an instance of RadarInstruction or throwing a WrongInstructionFormatException()
	 * @return Instruction Reference to an instance of RadarInstruction
	 * @throws WrongInstructionFormatException When the String is not RADAR
	 */
	public Instruction parse(String cad) throws WrongInstructionFormatException {
		
		StringTokenizer st = new StringTokenizer(cad, " ");
		String words = st.nextToken().toUpperCase();
		
		if(words.equals("RADAR")){
			if (!st.hasMoreTokens())
				return new RadarInstruction();
			else
				throw new WrongInstructionFormatException("WALL·E says: I do not understand. Please repeat" + LINE_SEPARATOR);
		}
		else
			throw new WrongInstructionFormatException("WALL·E says: I do not understand. Please repeat" + LINE_SEPARATOR);
	}

	/**
	 * Returns a description of the Instruction syntax. The string does not end 
	 * with the line separator. It is up to the caller adding it before printing.
	 * @return the Instruction syntax RADAR
	 */
	public String getHelp() {
		return "RADAR" + LINE_SEPARATOR;
	}

	/**
	 * Configure the context
	 * @param engine The robot engine
	 * @param navigation The information about the game, i.e., the places, current 
	 * direction and current heading to navigate
	 * @param robotContainer The inventory of the robot
	 */
	public void configureContext(RobotEngine engine,NavigationModule navigation, ItemContainer robotContainer) {
		this.navigation = navigation;	
	}

	/**
	 * Shows the current place description
	 * @throws InstructionExecutionException if there exist any execution error
	 */
	public void execute() throws InstructionExecutionException {
		
		if (navigation.getCurrentPlace()!=null)
			this.navigation.scanCurrentPlace();
		else
			throw new InstructionExecutionException();
		
	}

}
